#!/usr/bin/env bash
helpFunction()
{
    echo ""
    echo "USAGE: $0 -d 数据库名 -u 用户名 -p 密码"
    echo  -e "\t-d 数据库名"
    echo  -e "\t-u 用户名"
    echo  -e "\t-p 密码"
    exit 1
}


while getopts d:u:p:h: opt;
do
case $opt in
d) dbname="$OPTARG"
   ;;
u) username="$OPTARG"
   ;;
p) password="$OPTARG"
   ;;
h) host="$OPTARG"
   ;;
?) echo "$opt is an invalid option"
   ;;
esac
done
if [ -z "$host" ]
then
    echo '默认host'
    host="127.0.0.1"

fi

if [ -z "$dbname" ] || [ -z "$username" ] || [ -z "$password" ]
then
    echo "部分或全部参数为空";
    helpFunction
    exit 1;
fi

file='.env'
#安装项目依赖
composer install
old_d="DB_DATABASE=laravel-admin-2.0"
old_u="DB_USERNAME=www"
old_p="DB_PASSWORD=yuxingfei"
old_h="DB_HOST=mysql56"

new_d="DB_DATABASE="$dbname
new_u="DB_USERNAME="$username
new_p="DB_PASSWORD="$password
new_h="DB_HOST="$host
#写入配置文件
sed -i "" "s/$old_d/$new_d/g" "$file"
sed -i "" "s/$old_u/$new_u/g" "$file"
sed -i "" "s/$old_p/$new_p/g" "$file"
sed -i "" "s/$old_h/$new_h/g" "$file"

#数据库操作
php artisan migrate
php artisan db:seed
#创建软连接
php artisan storage:link

echo '安装完成 后台访问地址 http://域名/admin'

exit 1;
